﻿using System;

namespace Zinnia.Lite.Core.Imp
{
    public abstract class ZinniaLite : IZinniaLite
    {
    }

    public abstract class ZinniaLiteIdentity : ZinniaLite, IZinniaLiteIdentity
    {
        string _id;
        protected ZinniaLiteIdentity()
        {
            _id = Guid.NewGuid().ToString().Replace("-", "");
        }

        public virtual string Id
        {
            get { return _id; }
            set { _id = value; }
        }
    }

    public abstract class ZinniaBaseLite : ZinniaLiteIdentity, IZinniaBaseLite
    {
        private readonly Type _declareType;
        private DateTime _createTime;
        private readonly DateTime _currentTime;
        private DateTime _modifyTime;

        protected ZinniaBaseLite() : base()
        {
            _declareType = this.GetType();
            _currentTime = DateTime.Now;
            _createTime = _currentTime;
        }
        /// <summary>
        ///被赋值之后，创建时间将不在更改
        /// </summary>
        private bool IsNew => _currentTime == _createTime;

        public DateTime CreateTime
        {
            get
            {
                return _createTime;
            }
            set
            {
                if (IsNew)
                {
                    _createTime = value;
                }
            }
        }

        public DateTime ModifyTime
        {
            get { return _modifyTime == DateTime.MinValue ? DateTime.Now : _modifyTime; }
            set { _modifyTime = value; }
        }

        public virtual string GetTableName()
        {
            return _declareType.FullName.Substring(_declareType.FullName.LastIndexOf('.') + 1);
        }
        public virtual string GetSchemaName()
        {
            return "dbo";
        }
        public int Version { get; set; }
    }
}
